<?php

namespace app\money\controller;

use app\admin\common\Base;
use think\Db;
// 供应商对账单
class Suppliersreconciliation extends Base
{
    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            if ($map == '') {
                $list = Db::name('order')->where('big_type', 1)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where('user_id', $data['user_id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('id desc')->limit($start, $limit)->select();
                $count = Db::name('order')->where('big_type', 1)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where('user_id', $data['user_id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('id desc')->limit($start, $limit)->count();
            } else {
                $list = Db::name('order')->where('big_type', 1)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where('user_id', $data['user_id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('id desc')->limit($start, $limit)->select();
                $count = Db::name('order')->where('big_type', 1)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where('user_id', $data['user_id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('id desc')->limit($start, $limit)->count();
            }

            foreach ($list as $key => $value) {
                $list[$key]['dj_time'] = date('Y-m-d', $value['dj_time']);
                switch ($value['type']) {
                    case 0:
                        $list[$key]['type_name'] = '普通采购';
                        break;
                    case 1:
                        $list[$key]['type_name'] = '采购退回';
                        break;
                    default:
                        # code...
                        break;
                }
            }

            return $this->grid_data($list, $count, $page, $limit);
        }

        $month = $this->get_month_start_to_end();
        $company_name = Db::name('system_company')->where('id', 1)->value('company_name');
        $this->assign('month', $month);
        $this->assign('company_name', $company_name);

        return $this->fetch();
    }
}
